package com.org.zp.ctrl;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.ctrl.company.BusinessSchoolActivity;
import com.org.zp.ctrl.company.ComTalentPoolActivity;
import com.org.zp.ctrl.company.CompanyPostActivity;
import com.org.zp.ctrl.company.DeliveryCompanyPostActivity;
import com.org.zp.ctrl.company.ModifyComInfoActivity;
import com.org.zp.ctrl.job.JobActivity;
import com.org.zp.ctrl.login.LoginActivity;
import com.org.zp.entity.login.UserInfo;

public class ComMainActivity extends BaseActivity {

	private GridView gridview;

	private int[] pic;
	private Button top_right_btn;
	private RecApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_user);

		if (!(ActivityTaskManager.getInstance().containsName("ComMainActivity"))) {
			ActivityTaskManager.getInstance().putActivity("ComMainActivity",
					this);
		}

		app = getMyApplication();

		top_right_btn = (Button) findViewById(R.id.header_right);
		if ("".equals(app.user.getuID())) {
			top_right_btn.setText("登录");
		} else {
			top_right_btn.setText("注销");
		}

		top_right_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("登录".equals(top_right_btn.getText().toString())) {
					Intent intent = new Intent(ComMainActivity.this,
							LoginActivity.class);
					intent.putExtra("type", "0");
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity(
							"ComMainActivity");
				} else {
					top_right_btn.setText("登录");
					app.user = new UserInfo();

					Intent intent = new Intent(ComMainActivity.this,
							UserMainActivity.class);
					startActivity(intent);
				}

			}
		});

		pic = new int[] { R.drawable.main_com_news, R.drawable.main_com_modify,
				R.drawable.main_com_position, R.drawable.main_com_pool,
				R.drawable.main_com_cast, R.drawable.main_com_college };

		gridview = (GridView) findViewById(R.id.main_gridview);
		gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));

		ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < pic.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", pic[i]);// 添加图像资源的ID
			lstImageItem.add(map);
		}
		// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		SimpleAdapter saImageItems = new SimpleAdapter(this, // 没什么解释
				lstImageItem,// 数据来源
				R.layout.main_grid_item,// night_item的XML实现

				// 动态数组与ImageItem对应的子项
				new String[] { "ItemImage" },

				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.main_grid_img });
		// 添加并且显示
		gridview.setAdapter(saImageItems);
		// 添加消息处理
		gridview.setOnItemClickListener(new ItemClickListener());
	}

	// 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	class ItemClickListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent;
			switch (arg2) {
			case 0:
				intent = new Intent(ComMainActivity.this, JobActivity.class);
				startActivity(intent);
				break;
			case 1:
				intent = new Intent(ComMainActivity.this,
						ModifyComInfoActivity.class);
				startActivity(intent);
				break;
			case 2:
				intent = new Intent(ComMainActivity.this,
						CompanyPostActivity.class);
				startActivity(intent);
				break;
			case 3:
				intent = new Intent(ComMainActivity.this,
						ComTalentPoolActivity.class);
				startActivity(intent);
				break;
			case 4:
				intent = new Intent(ComMainActivity.this,
						DeliveryCompanyPostActivity.class);
				startActivity(intent);
				break;
			case 5:
				intent = new Intent(ComMainActivity.this,
						BusinessSchoolActivity.class);
				startActivity(intent);
				break;
			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"ComMainActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
